package tech.summerly.weather.db

import android.arch.persistence.room.Room
import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import tech.summerly.weather.data.local.RegionDataBase

@RunWith(AndroidJUnit4::class)
class RegionDaoTest {

    private val database = Room
            .inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), RegionDataBase::class.java)
            .allowMainThreadQueries()
            .build()

    private val regionDao = database.regionDao()


    @Test
    fun getRegionWhenNoRegionInsert() {
        val products = regionDao.getRegions()
        assertTrue(products.isEmpty())
    }

    @Test
    fun getRegionAfterRegionInsert() {
        regionDao.insertRegion(DummyRegion.dummyRegion)
        val regions = regionDao.getRegions()
        assertArrayEquals(regions.toTypedArray(), DummyRegion.dummyRegion.toTypedArray())
    }


    @Test
    fun queryRegion() {
        regionDao.insertRegion(DummyRegion.dummyRegion)

        val result = regionDao.queryRegion("定海")
        assertTrue(result.size == 1)
        assertEquals(result[0], DummyRegion.dingHai)
    }


    @After
    fun closeDb() {
        database.close()
    }
}